/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.home.dao;

import com.home.common.utils.HibernateUtil;
import com.home.mapping.Agent;
import java.util.List;
import java.util.Set;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pnduy
 */
public class BaseDao {

    public BaseDao() {
    }

    public boolean saveOrUpdate(Object obj) {
        boolean res = true;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            session.saveOrUpdate(obj);
            trans.commit();
        } catch (HibernateException he) {
            trans.rollback();
            res = false;
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return res;
    }

    public boolean delete(Object obj) {
        boolean res = true;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            session.delete(obj);
            trans.commit();
        } catch (HibernateException he) {
            trans.rollback();
            res = false;
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return res;
    }

    public Object findByPk(Class clas, Integer pk) {
        Object obj = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            obj = session.load(clas, pk);
            Hibernate.initialize(obj);
        } catch (HibernateException he) {
            he.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return obj;
    }

    public List findAll(Class clas) {
        List list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            list = session.createQuery("FROM " + clas.getName()).list();
        } catch (HibernateException he) {
            he.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
    
    public boolean insertSetObject(Set<Object> setObjs) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = null;
        boolean returnValue = true;
        try {
            trans = session.beginTransaction();
            
            for (Object obj : setObjs) {
                session.save(obj);
            }
            trans.commit();
            
        }catch (Exception e) {
            e.printStackTrace();
            returnValue = false;
            trans.rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return returnValue;
    }
    
}
